package com.jinyijia.api.rest.v2.goods;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author yangrd
 * @version 1.0.0
 * @date 2018/10/6
 **/
@RestController
@RequestMapping("/v2/goods")
public class GoodsDetailsController {

    private RestTemplate restTemplate;

    @Autowired
    public GoodsDetailsController(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    @GetMapping("/{id}")
    public GoodsDetailsVO get(@PathVariable Long id) {
        GoodsDetailsVO detailsVO = restTemplate.getForObject("http://goods-service/goods/{1}", GoodsDetailsVO.class, id);
        List parameter = restTemplate.getForObject("http://product-service/products/{1}", JSONObject.class, detailsVO.getProductId()).getJSONArray("groupSet");
        Map<String, Set<String>> specifications = detailsVO.getSpecifications().entrySet().stream().collect(Collectors.toMap(entry -> this.replaceN(entry.getKey()), entry -> entry.getValue().stream().map(this::replaceN).collect(Collectors.toSet())));
        detailsVO.setSpecifications(specifications);
        detailsVO.setParameter(parameter);
        detailsVO.setFreightInfo(getFreightStr(detailsVO.getShopId()));
        detailsVO.setTransactionNum(1000);
        return detailsVO;
    }

    @GetMapping("/{goodsId}/sku")
    public List<SkuDTO> listSku(@PathVariable Long goodsId) {
        List<SkuDTO> skuDTOList = restTemplate.exchange("http://goods-service/goods/{1}/skus", HttpMethod.GET, null, new ParameterizedTypeReference<List<SkuDTO>>() {
        }, goodsId).getBody();
        // 去掉空格
        skuDTOList.forEach(skuDTO -> skuDTO.setAttributes(skuDTO.getAttributes().entrySet().stream().collect(Collectors.toMap(entry -> this.replaceN(entry.getKey()), entry -> this.replaceN(entry.getValue())))));
        return skuDTOList;
    }


    @GetMapping("freight-price")
    public Integer getFreightPrice(Integer shopId, Integer totalNum, Integer totalPrice) {
        FreightDTO freightDTO = restTemplate.getForObject("http://merchant-service/merchants/freight/{1}/one", FreightDTO.class, shopId);
        Integer type = freightDTO.getType();
        if (type.equals(0)) {
            return 0;
        } else if (type.equals(1)) {
            return freightDTO.getCount() <= totalNum ? 0 : freightDTO.getPrice() / 100;
        } else if (type.equals(2)) {
            return freightDTO.getCount()/100 <= totalPrice ? 0 : freightDTO.getPrice() / 100;
        } else if (type.equals(3)) {
            return freightDTO.getPrice() / 100;
        }
        return 0;
    }

    private String getFreightStr(Long shopId) {
        FreightDTO freightDTO = restTemplate.getForObject("http://merchant-service/merchants/freight/{1}/one", FreightDTO.class, shopId);
        Integer type = freightDTO.getType();
        if (type.equals(0) || type.equals(3)) {
            return freightDTO.getName();
        } else if (type.equals(1)) {
            return String.format(" %s元 (%s件起免运费)", freightDTO.getPrice() / 100, freightDTO.getCount());
        } else if (type.equals(2)) {
            return String.format(" %s元 (满%s元免运费)", freightDTO.getPrice() / 100, freightDTO.getCount() / 100);
        }
        return "";
    }

    private String replaceN(String str) {
        return str.replaceAll("\n", "").trim();
    }

}
